﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using JPCloudPrinter.Utils;

namespace JPCloudPrinter {
    public partial class FrmMain : CCSkinMain {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //初始化窗口出现位置
            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 50, (Screen.PrimaryScreen.WorkingArea.Height - this.Height)/2);
            this.PointToScreen(p);
            this.Location = p;

            ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ShowInformation),null);
        }

        private void ShowInformation(object obj)
        {
            Thread.Sleep(2000);

            this.Invoke(new MethodInvoker(()=>
            {
                FrmInformation frm = new FrmInformation("通知");
                frm.Show(this);
            }));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            //显示主窗体
            this.Visible = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;
            //隐藏托盘图标
            this.notifyIcon1.Visible = false;
            this.Show();
        }

        private void ViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string url = "http://www.sudaonline.net:8080/wzzgh-upload/upload/paper/2017/1/6efde517-c5f7-470f-8a76-899709367d23.pdf";
            //string displayName = "工人报.pdf";

            //获取文件信息
            ContentInfo cif = HttpUtil.Head(url);
            DateTime lastModified = cif.LastModified;
            string fileName = url.Substring(url.LastIndexOf("/")+1);
            string folder = Application.StartupPath + "\\download\\" + lastModified.Year + "\\" + lastModified.Month;
            string filePath = folder + "\\" + fileName;

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            //首先判断本地是否已下载
            if (File.Exists(filePath))
            {
                FileInfo fif = new FileInfo(filePath);

                if (fif.Length == cif.ContentLength)
                {
                    //打开文件
                    System.Diagnostics.Process.Start("explorer", filePath);
                }
            }
            else
            {
                FrmDownload frm = new FrmDownload(url, filePath);
                frm.ShowDialog();
            }
        }
    }
}
